﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IComobox</summary>
	public class IComboBox : System.Windows.Forms.ComboBox
	{
        protected System.Collections.Generic.List<int> FIntIDs;
        protected System.Collections.Generic.List<string> FStringIDs;

        /// <summary>Property: SelectedIntID</summary>
        public int SelectedIntID
        {
            get
            {
                int I = SelectedIndex; // Ziskam index vybranej polozky
                return I == -1 ? -1 : FIntIDs[I]; // Vratim ID
            }
            set
            {
                int I = FIntIDs.IndexOf(value);
                if (I != -1) SelectedIndex = I;
            }
        }
        /// <summary>Property: SelectedStringID</summary>
        public string SelectedStringID
        {
            get
            {
                int I = SelectedIndex; // Ziskam index vybranej polozky
                return I == -1 ? "" : FStringIDs[I]; // Vratim ID
            }
            set
            {
                int I = FStringIDs.IndexOf(value, 0);
                if (I != -1) SelectedIndex = I;
            }
        }
        /// <summary>Property: FirstID</summary>
        public int FirstID
        {
            get
            {
                return FIntIDs.Count == 0 ? 0 : FIntIDs[0];
            }
        }

        /// <summary>Konstruktor</summary>
        /// <param name="pStyle">defaultny styl</param>
        public IComboBox(System.Windows.Forms.ComboBoxStyle pStyle = System.Windows.Forms.ComboBoxStyle.DropDownList)
        {
            DropDownStyle = pStyle;
            FIntIDs = new System.Collections.Generic.List<int>();
            FStringIDs = new System.Collections.Generic.List<string>();
        }

        /// <summary>Zaradenie polozky</summary>
        /// <param name="pText">text</param>
        /// <param name="pID">priradene ID</param>
        /// <returns>pozicia</returns>
        public int AddItem(string pText, int pID)
        {
            FIntIDs.Add(pID);
            return Items.Add(pText);
        }
        /// <summary>Zaradenie polozky</summary>
        /// <param name="pText">text</param>
        /// <param name="pID">priradene ID</param>
        /// <returns>pozicia</returns>
        public int AddItem(string pText, string pID)
        {
            FStringIDs.Add(pID);
            return Items.Add(pText);
        }
        /// <summary>Zaradenie viacerich poloziek</summary>
        /// <param name="pTexts">testy poloziek</param>
        /// <param name="pSeparator">oddelovac textov</param>
        /// <param name="pIDs">pole IDciek</param>
        public void AddMultiItems(string pTexts, char pSeparator, int[] pIDs)
        {
            string[] Strs = pTexts.Split(new char[] { pSeparator }); // Rozdelim na polozky
            for (int i = 0; i < Strs.Length; i++) // Zaradim vsetky
            {
                AddItem(Strs[i], pIDs[i]);
            }
        }
    }

    /// <summary>Trieda IComboBox_ThreeState</summary>
    public class IComboBox_ThreeState : IComboBox
    {
        /// <summary>Konstruktor</summary>
        /// <param name="pItems">nazvy troch stavov</param>
        /// <param name="pLeft"></param>
        /// <param name="pTop"></param>
        /// <param name="pWidth"></param>
        /// <param name="pHeight"></param>
        /// <param name="pPositionType">typ umiestnenia</param>
        /// <param name="pControl">control pre umiestnenie</param>
        public IComboBox_ThreeState(string pItems, char pItemsSeparator = '\b')
            : base(System.Windows.Forms.ComboBoxStyle.DropDownList)
        {
            Items.Add(ICore.GetSubstring(pItems, 0, pItemsSeparator)); // Vytvorim polozky
            Items.Add(ICore.GetSubstring(pItems, 1, pItemsSeparator));
            Items.Add(ICore.GetSubstring(pItems, 2, pItemsSeparator));
        }

        /// <summary>Property: Value</summary>
        public int Value
        {
            get
            {
                switch (SelectedIndex)
                {
                    case 1: return ICore.TS_TRUE;
                    case 2: return ICore.TS_FALSE;
                    default: return ICore.TS_NONE;
                }
            }
            set
            {
                switch (value)
                {
                    case ICore.TS_TRUE: SelectedIndex = 1; break;
                    case ICore.TS_FALSE: SelectedIndex = 2; break;
                    default: SelectedIndex = 0; break;
                }
            }
        }
    }
}
